#!/usr/bin/python
import signal
import urllib
import fetch
import time
import atexit
import sys

import character

def cleanup(thread):
    thread.kill()


def fetchtest():
    pool = fetch.PoolThread(2, 5)

    # Cleanup properly on kill and program exit
    atexit.register(cleanup, pool)
    signal.signal(signal.SIGTERM, lambda signum, stack_frame: sys.exit(1))
    signal.signal(signal.SIGINT, lambda signum, stack_frame: sys.exit(1))

    print "start"
    pool.start()

    # Test account? pulled from example
    params = urllib.urlencode( {
        'characterID': 150209812,
        'userid': 256833,
        'apikey': 'DVPtgNgmyEGk9L9RxDTJn8dCjIraiaa7efNK4VlGKAoI6Hm8uB514zy6CbFCuScC',
        } )

    print "push"
    pool.pushURL("/char/WalletTransactions.xml.aspx", params, fetch.
        FETCH_PERIODIC, True)
    pool.pushURL("/account/Characters.xml.aspx", params, fetch.
        FETCH_NEVER, False)

    time.sleep(5)
    print "refetch all"
    pool.refetch(True)

    while True:
        continue


def parsetest():
    charsheet = open("CharacterSheet.xml", "r")
    sit = open("SkillInTraining.xml", "r")
    mychar = character.Character(charsheet)
    mychar.parseSIT(sit)

    stree = character.SkillTree(open("SkillTree.xml", "r"))

if __name__ == "__main__":
    parsetest()
